/**
 * 慧眼识珠 游戏 时间耗尽
 */
import GameScene from "./GameScene";
import gameShareAdsVideo from "./SdkTools/gameShareAdsVideo";
import PlayData, { GameType } from "./SdkTools/PlayData";
import storage from "./SdkTools/storage";
import TipsManager from "./SdkTools/TipsManager";
import TopUi from "./TopUi";

const { ccclass, property } = cc._decorator;

@ccclass
export default class timeOVer extends cc.Component {
    @property(cc.Node)
    kuang0: cc.Node = null
    @property(cc.Node)
    kuang1: cc.Node = null
    @property(cc.Node)
    kuang2: cc.Node = null
    @property(cc.Node)
    kuang3: cc.Node = null
    @property(cc.Label)
    cha: cc.Label = null
    @property(cc.Label)
    challengenum: cc.Label = null
    @property(cc.Node)
    getchancebtn: cc.Node = null
    @property(cc.Node)
    again: cc.Node = null
    @property(cc.Node)
    faile: cc.Node = null
    @property(cc.Node)
    success: cc.Node = null
    @property(cc.Sprite)
    title: cc.Sprite = null
    private overType: number = -1
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        gameShareAdsVideo.Instance.recording_Video_End()

        this.kuang0.active = !1
        this.kuang1.active = !1
        this.kuang2.active = !1
        this.kuang3.active = !1
        if (this.overType == 0) {
            this.kuang0.active = !0
            this.kuang1.active = !1
            this.kuang2.active = !1
            this.kuang0.opacity = 123
            this.kuang0.scale = 0.3
            this.faile.active = !1
            this.success.active = !1
            cc.tween(this.kuang0)
                .to(0.1, { opacity: 255, scale: 1 })
                .call(() => {
                    cc.director.pause()
                })
                .start()
            cc.director.emit('fail')

        } else if (this.overType == 2) {
            this.kuang2.active = !0
            this.kuang2.scale = 0.3
            cc.tween(this.kuang2)
                .to(0.1, { scale: 1 })
                .call(() => {
                    cc.director.pause()
                })
                .start()
            cc.director.emit('fail')
            console.log('复活')
        } else if (this.overType == 3) {
            this.kuang3.active = !0
            this.kuang3.scale = 0.3
            cc.tween(this.kuang3)
                .to(0.1, { scale: 1 })
                // .call(()=>{
                //     cc.director.pause()
                // })
                .start()

            let gold = storage.Instance.getGold() + 10
            storage.Instance.setGold(gold)
            let top = cc.find('Canvas/top')
            let topsc = top.getComponent(TopUi)
            topsc.setGold()
        }
        else {
            cc.director.emit('win')
            this.kuang0.active = !1
            this.initKuang1()
        }
    }

    protected onEnable(): void {
        gameShareAdsVideo.Instance.bannerAds_Show()
        gameShareAdsVideo.Instance.chaPingAds_Show()
    }

    protected onDisable(): void {
        gameShareAdsVideo.Instance.bannerAds_Hide()
    }

    setType(type: number) {
        this.overType = type
    }

    reliveBtnClick() {
        var self = this
        cc.director.resume();  // 确保游戏不在暂停状态
        gameShareAdsVideo.Instance.motivational_Video_Show(() => {
            if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
                let gamenode = cc.find('Canvas')
                let sc = gamenode.getComponent(GameScene)
                sc.reliveGame()
            }
            self.node.destroy()
        }, () => {
            console.log('广告未完成，复活未执行');
        })
    }



    initKuang1() {
        this.kuang1.active = !0

        // this.kuang1.opacity = 123
        // this.kuang1.scale = 0.3
        // cc.tween(this.kuang1)
        // .to(0.1,{opacity:255,scale:1})
        // .start()

        if (this.overType == 0 || this.overType == 2) {
            this.faile.active = !0
            this.success.active = !1
        } else {
            this.faile.active = !1
            this.success.active = !0
        }
    }

    closeKuang0() {
        cc.director.resume()
        this.kuang0.active = !1
        this.initKuang1()
    }
    closeKuang2() {
        cc.director.resume()
        this.kuang2.active = !1
        this.initKuang1()
    }

    againBtn(t, e) {
        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
            if (e == 'again') {
                if (storage.Instance.getEnergy() <= 0) {
                    TipsManager.Instance.createTips('体力不足')
                    let topsc = cc.find('Canvas/top').getComponent(TopUi)
                    topsc.openAddEngry()
                } else {
                    this.node.destroy()
                    cc.director.resume()
                    this.enterGame4Scene(-1)

                }
            } else if (e == 'again2') {
                var self = this
                gameShareAdsVideo.Instance.motivational_Video_Show(() => {
                    self.node.destroy()
                    self.enterGame4Scene(1)
                    cc.director.resume()
                }, () => { })
            }
        }
    }

    /**
     * 进入找你妹
     * @param num 
     */
    enterGame4Scene(num) {
        console.log('再次进入游戏', num)
        storage.Instance.setEnergy(storage.Instance.nowEnery + num)
        let topsc = cc.find('Canvas/top').getComponent(TopUi)
        topsc.setTiLi()
        let gamenode = cc.find('Canvas')

        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
            let sc = gamenode.getComponent(GameScene)
            sc.initGame()
        }
    }

    start() {

    }

    shareGame() {
        gameShareAdsVideo.Instance.recording_Video_Share(() => { }, () => { })
    }

    backToStartScene() {
        cc.director.resume()
        cc.director.loadScene('StartScene')
    }

    // update (dt) {}
}
